package com.luocj.looook.ui.progressview;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;

/**
 * Author White
 * Date 2017/4/23
 * Time 14:27
 */

public class Utils {

    public static int dp2px(Context context, int dpVal) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpVal * scale + 0.5f);
    }

    public static int sp2px(Context context, int spVal) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spVal * fontScale + 0.5f);
    }

    public static void animDown(Context context, final View view) {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), view.getTranslationY() + Utils.dp2px(context, 50));
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", view.getScaleY(), view.getScaleY() * 1.0f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", view.getScaleY(), view.getScaleY() * 1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(translationY, scaleY, scaleX);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                view.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                view.setClickable(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animatorSet.setDuration(500);
        animatorSet.start();
    }

    public static void animUp(Context context, final View view) {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), view.getTranslationY() - Utils.dp2px(context, 50));
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", view.getScaleY(), view.getScaleY() * 1.0f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", view.getScaleY(), view.getScaleY() * 1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                view.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                view.setClickable(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animatorSet.playTogether(translationY, scaleY, scaleX);
        animatorSet.setDuration(500);
        animatorSet.start();
    }

}

